package com.eryos.android.holidaybudget;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.eryos.android.holidaybudget.db.BudgetDatabase;
import com.eryos.android.holidaybudget.tools.WaitingMessage;
import com.eryos.android.holidaybudget.ui.TopBanner;

/**
 * Project :  HolidayBudget
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/HolidayBudget/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public abstract class RootActivity extends Activity {

	public static String LOG_TAG = "";
	public enum VIEW{HOME, BUDGET, EXPENSE, SUMMARY};

	protected ImageView backPicture;
	protected ImageView waitPicture;
	protected TopBanner banner;
	
	protected String viewTitle = "Welcome";
	protected boolean hasTopBanner = false;
	protected boolean hasBackButton = true;

	protected static BudgetDatabase db;

	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LOG_TAG = getLocalClassName();//this.getClass().getSimpleName();

        WaitingMessage.init(this);

        /*  */
        initParam();

        /* Init UI (Layout, UI Components, etc.) */
        initUI();

        if ( hasTopBanner ){
            banner = new TopBanner(this, hasBackButton, viewTitle);
            LinearLayout mainLayout		= (LinearLayout) findViewById(R.id.main_layout);
            mainLayout.addView(banner, 0);
        }

        /*  */
        initDB();

        /* Init all UI Handlers (Click, Select, etc.) */
        initHandlers();

        /* Retrieve data from source (DB, WebService, File, etc.) */
        fetchData();
	}
	
    /**
     * Method used to display another view ( Home, Stats, Settings )
     * @param _view : the new view to display
     */
    public void loadView(VIEW _view){
    	Log.d(LOG_TAG, "loadView :"+_view);
    	/* Show splash screen */
    	showWaitingMessage();

    	Class dest = this.getClass();
    	switch(_view){
    	case HOME:
    		dest = Main.class;
    		break;
    	case BUDGET:
    		dest = BudgetActivity.class;
    		break;
    	case EXPENSE:
    		dest = ExpenseActivity.class;
    		break;
    	case SUMMARY:
    		dest = SummaryActivity.class;
    		break;
    	}
 
    	Intent intent = new Intent(RootActivity.this, dest);
    	startActivity(intent);

    	if ( ! (this instanceof Main) )
    		finish();
    }

    /**
     * Method used to initialize the DB connection
     */
    private void initDB(){
    	Log.d(LOG_TAG, "initDB");
    	if ( db == null ){
    		db = BudgetDatabase.getInstance(this);        
    		db.open();
    	}
    }
    

	
	protected void showWaitingMessage(){
		WaitingMessage.showWaitingMessage();
	}
	
	protected void hideWaitingMessage(){
		WaitingMessage.hideWaitingMessage();
	}
	
	protected void hideWaitingMessage(String value){
		WaitingMessage.hideWaitingMessage(value);
	}
	
	protected void hideWaitingMessage(int id){
		WaitingMessage.hideWaitingMessage(id);
	}
	
	protected abstract void initParam();
	
	protected abstract void initUI();

	protected void initHandlers(){
		Log.d(LOG_TAG, "initHandlers");
		if ( hasTopBanner ){
            backPicture = (ImageView) findViewById(R.id.banner_back_picture);

        	backPicture.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	Log.d(LOG_TAG, "Click Back Picture");
	            	loadView(VIEW.HOME);
	            }
        	});
        }


	}
	
	protected abstract void fetchData();
	
	protected abstract void refreshUI();
	
	
	
	/**/
	
    @Override
    public void onResume() {
    	super.onResume();
    	Log.d(getLocalClassName(), "onResume");
//    	db.open();
    }
    
    @Override
    public void onStart() {
    	super.onStart();
        Log.d(getLocalClassName(), "onStart");
//    	db.open();
    }
    
    @Override
    public void onRestart() {
    	super.onRestart();
    	Log.d(getLocalClassName(), "onRestart");
//    	db.open();
    	
//    	initTracker();

    	/* show splashscreen during complex treatment */
//        showWaitingMessage();
		
        /* Process long/complex treatments in separate thread */


    }

    @Override
    protected void onDestroy() {
//    	if ( adView != null ) 
//    		adView.destroy();
    	super.onDestroy();
    	Log.d(getLocalClassName(), "onDestroy");
    	hideWaitingMessage();
//    	GoogleAnalyticsProvider.stopTracker();
    }
    
    @Override
    protected void onPause() {
      super.onPause();
      Log.d(getLocalClassName(), "onPause");
      hideWaitingMessage();
      //GoogleAnalyticsProvider.stopTracker();
    }	
    
    @Override
    protected void onStop() {
      super.onStop();
      Log.d(getLocalClassName(), "onStop");
    }	
	
}
